package org.fxml.utils {

	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * @author jordandoczy
	 */
	public class Timer extends flash.utils.Timer implements ITimer {
		
		protected var _index:int = 0;
		protected var _upperBound:int = -1;
		
		public function Timer(delay:Number, repeatCount:int=0, upperBound:int=-1){
			super(delay, repeatCount);
			_upperBound = upperBound;
			
			addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
		}

		public function get upperBound():int{
			return _upperBound;
		}
		
		public function set upperBound(value:int):void{
			_upperBound = value;
		}
		
		[Bindable]
		public function get index():int{
			return _index;	
		}
		
		public function set index(value:int):void{
			if(value == _index) return;
			
			if (_upperBound != -1 && value >= _upperBound) value = 0;
			
			_index = value;
			dispatchEvent(new Event(Event.CHANGE));
			reset();
			start();
		}
		
		protected function onTimer(event:TimerEvent):void{
			index++;
		}
		
	}
}
